package com.yiliao.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.yiliao.service.PayDeployService;
import com.yiliao.util.MessageUtil;
import com.yiliao.util.RedisUtil;
import com.yiliao.util.SystemConfig;

@Service
public class PayDeployServiceImpl extends ICommServiceImpl implements PayDeployService {

	@Autowired
	RedisUtil redisUtil;
	
	@Override
	public Map<String, Object> getPayDeployList(int page) {

		Map<String, Object> resultToMap = this.getFinalDao().getIEntitySQLDAO()
				.findBySQLUniqueResultToMap("SELECT COUNT(1) totalCount FROM t_pay_dispose ");

		// 获取列表

		List<Map<String, Object>> sqltoMap = this.getFinalDao().getIEntitySQLDAO()
				.findBySQLTOMap("SELECT * FROM t_pay_dispose LIMIT ?,10", (page - 1) * 10);

		return new HashMap<String, Object>() {
			private static final long serialVersionUID = 1L;
			{
				put("total", resultToMap.get("totalCount"));
				put("rows", sqltoMap);
			}
		};
	}

	@Override
	public MessageUtil addOrPayDeploy(Integer t_id, String payName, int isDefault, int isScrap, int payType) {
		try {
			// 新增数据
			//-1 支付宝 -2 微信
			String icon=SystemConfig.getValue("pay_icon_alipay");
			if(payType==-2||payType==-5) {
				icon=SystemConfig.getValue("pay_icon_weixin");
			}
			if(payType == -7){
				icon = SystemConfig.getValue("pay_icon_ace");
			}
			if(payType ==  -8){
				icon = SystemConfig.getValue("pay_icon_swapz");
			}
			if(payType ==  -9){
				icon = SystemConfig.getValue("pay_icon_weixin");
			}

			if(payType ==  -10){
				icon = SystemConfig.getValue("pay_icon_alipay");
			}
			if (null == t_id || 0 == t_id) {
				//判断是否将该数据设置为默认
				if(1 == isDefault) {
					//所有数据设置为非默认
					this.getFinalDao().getIEntitySQLDAO().executeSQL("UPDATE t_pay_dispose SET t_is_default = -1");
				}

				this.getFinalDao().getIEntitySQLDAO().executeSQL(
						"INSERT INTO t_pay_dispose (t_pay_name, t_pay_icon, t_is_default, t_is_scrap, t_pay_type) VALUES ( ?, ?, ?, ?, ?);",
						payName, icon,
						isDefault, isScrap, payType);

			} else { // 修改数据

				//判断是否将该数据设置为默认
				if(1 == isDefault) {
					//所有数据设置为非默认
					this.getFinalDao().getIEntitySQLDAO().executeSQL("UPDATE t_pay_dispose SET t_is_default = -1");
				}
				
				this.getFinalDao().getIEntitySQLDAO().executeSQL(
						"UPDATE t_pay_dispose SET t_pay_name=?, t_pay_icon=?, t_is_default=?, t_is_scrap=?, t_pay_type=? WHERE t_id=?;",
						payName, icon,
						isDefault, isScrap, payType, t_id);

			}

			return new MessageUtil(1, "保存成功!");
		} catch (Exception e) {
			e.printStackTrace();
			return new MessageUtil(0, "程序异常!");
		}

	}

	/**
	 * 删除配置
	 */
	@Override
	public MessageUtil delPayDeploy(int t_id) {
		try {
		
			//判断这个ID是否有支付密钥在使用
			Map<String, String> hmget = redisUtil.hmget("pay_deploy_"+t_id);
			
			if(null != hmget && !hmget.isEmpty()) {
				return new MessageUtil(1, "该支付设置已配置支付密钥!请先删除支付密钥.");
			}
		
			this.getFinalDao().getIEntitySQLDAO().executeSQL("DELETE FROM t_pay_dispose WHERE t_id = ? ", t_id);
			
			return new MessageUtil(1, "删除成功!");
		} catch (Exception e) {
			e.printStackTrace();
			return new MessageUtil(0, "程序异常!");
		}
	}

	
	
}
